package com.company.project.controller;

import com.company.project.common.utils.DataResult;
import com.company.project.entity.VaccinationStatisticsEntity;
import com.company.project.service.VaccinationStatisticsService;
import com.company.project.vo.req.VaccinationStatisticsTdVO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * ${comments}
 *
 * @author jiapeiyi
 * @email *****@mail.com
 * @date 2021年4月20日
 */
@Controller
@RequestMapping("/")
public class VaccinationStatisticsController {
    @Resource
    private VaccinationStatisticsService vaccinationStatisticsService;


    /**
     * 跳转到页面
     */
    @GetMapping("/index/vaccinationstatistics")
    public String vaccinationstatistics() {
        return "vaccination/statislist";
    }

    /**
     * 跳转到页面
     */
    @GetMapping("/index/vstable")
    public String vstable() {
        return "vaccination/statislisttable";
    }


    /**
     * 得到行政区的统计数据
     */
    @ResponseBody
    @PostMapping("/vaccination/statisticslist")
    public DataResult list(@RequestBody Map<String, Object> params) {
        String enddate = "";
        enddate = params.get("enddate").toString();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        StringBuffer today = new StringBuffer();
        today.append(df.format(new Date()));
        List<VaccinationStatisticsEntity> list = vaccinationStatisticsService.qListByDate(enddate.equals("") ? today.toString() : enddate);
        return DataResult.success(list);
    }

    /**
     * 村级统计数据
     */
    @ResponseBody
    @PostMapping("/vaccination/villagelist")
    public DataResult villagelist(@RequestBody Map<String, Object> params) {
        String enddate = "";
        String jurisdiction = "";
        enddate = params.get("enddate").toString();
        jurisdiction = params.get("xzqh").toString();
        if(!isNumber(jurisdiction)){
            List<VaccinationStatisticsTdVO> v = vaccinationStatisticsService.todayCount(jurisdiction);
            jurisdiction = v.get(0).getMc();
        }
        List<VaccinationStatisticsEntity> list = vaccinationStatisticsService.cListBydate(jurisdiction, enddate);
        return DataResult.success(list);
    }

    /**
     * 村
     */
    @ResponseBody
    @PostMapping("/vaccination/todayCount")
    public DataResult todayCount(@RequestBody String date) {
        List<VaccinationStatisticsTdVO> list = vaccinationStatisticsService.todayCount(date);
        return DataResult.success(list);
    }

    public Boolean isNumber(String str){
        for (int i = 0; i < str.length(); i++){
            System.out.println(str.charAt(i));
            if (!Character.isDigit(str.charAt(i))){
                return false;
            }
        }
        return true;
    }

}
